// 存储reducer
// 用来管理页面标题的reducer
import { combineReducers } from "redux";
import storage from "../utils/storageUtils";
import { SET_HEAD_TITLE, RECEIVE_USER, SHOW_ERROR_MSG } from "./action-type";

const initHeadTitle: any = "首页";
function headTitle(state = initHeadTitle, action: { type: any; data: any }) {
  switch (action.type) {
    case SET_HEAD_TITLE:
      return action.data;
    default:
      return state;
  }
}

// 初始读一下用户的信息
const initUser: any = storage.getUser();
function user(
  state = initUser,
  action: { type: any; user: any; errorMsg: any }
) {
  switch (action.type) {
    case RECEIVE_USER:
      return action.user;
    case SHOW_ERROR_MSG:
      const errorMsg = action.errorMsg;
      return { ...state, errorMsg };
    default:
      return state;
  }
}

const Reducer = combineReducers({ headTitle, user });
export default Reducer;
